/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using Gtk;
using System.Collections;
using System.Collections.Generic;

namespace Whitemice.Calendar
{
  /* Displays a daystrip with a header and makes the day strip
     scrollable */
  [System.ComponentModel.Category("Calendar")]
  [System.ComponentModel.ToolboxItem(true)]
  public class DecoratedDayStrip : Gtk.VBox
  {
    protected DayStrip                    strip;
    protected DayStripHeader              header;
    protected ScrolledWindow              sw;
    public    CalendarContextHandler      OnStripContext;
    public    CalendarEventContextHandler OnEventContext;
    public    CalendarEventDropHandler    OnEventDrop;
    
    public DecoratedDayStrip() : base()
    {
      strip = new DayStrip();
      header = new DayStripHeader(strip);
      sw = new ScrolledWindow();
      sw.HscrollbarPolicy = PolicyType.Automatic;
      sw.VscrollbarPolicy = PolicyType.Always;
      sw.AddWithViewport(strip);
      this.PackStart(header, false, true, 2);
      this.PackStart(sw, true, true, 2);
      
      strip.OnEventContext += new CalendarEventContextHandler(this.CalendarEventContextHandler);
      strip.OnEventDrop += new CalendarEventDropHandler(this.CalendarEventDropHandler);
      strip.OnStripContext += new CalendarContextHandler(this.CalendarContextHandler);
    } /* end DecoratedDayStrip ctor */
    
    public Adjustment Hadjustment 
    {
      get { return sw.Hadjustment; }
    }
    
    public Adjustment Vadjustment 
    {
      get { return sw.Vadjustment; }
    }    
    
    public double PositionOfHour(int _hour)
    { return strip.PositionOfHour(_hour); }
    
    public void SetPositionToCurrent()
    {
      int hour = DateTime.Now.Hour;
      Vadjustment.Value = PositionOfHour(hour);
    }
    
    public int Span
    {
      get { return strip.Span; }
      set
      {
        if ((strip.Span != value) &&
            (value > 0) &&
            (value < 15))
        {
          strip.Span = value;
          header.Repaint();
        }
      }
    } /* end Span */
    
    public DateTime FirstDay
    {
      get { return strip.FirstDay; }
      set
      {
        if (strip.FirstDay != value)
        {
          strip.FirstDay = value;
          header.Repaint();
        }
      }
    } /* end FirstDay */
    
    public DateTime LastDay
    {
      get { return strip.LastDay; }
    }
    
    public IList<CalendarEvent> CalendarEvents 
    { 
      get { return strip.CalendarEvents; } 
    }
    
    protected virtual void CalendarEventContextHandler(CalendarEvent _event)
    {
      Console.WriteLine("CalendarEventContextHandler");
      if (OnEventContext != null)
        OnEventContext(_event);
    }
    
    protected virtual void CalendarEventDropHandler(CalendarEvent _event, DateTime _start)
    {
      Console.WriteLine("CalendarEventDropHandler");
      if (OnEventDrop != null)
        OnEventDrop(_event, _start);
    }
    
    protected virtual void CalendarContextHandler(DayStrip _strip, DateTime _current)
    {
      Console.WriteLine("CalendarContextHandler");
      if (OnStripContext != null)
        OnStripContext(_strip, _current);
    }
  }
}
